package com.sesame.user.service;

import com.sesame.user.bean.User;
import kim.sesame.framework.cache.annotation.QueryCache;
import kim.sesame.common.web.context.SpringContextUtil;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@CommonsLog
public class UserService {

    //    @Cacheable(value = "userlist", key = "'userlist'.concat(#id.toString())")
    @Cacheable(value = "userlist", key = "#id")
    public List<User> searchList(String id) {
        List<User> list = Arrays.asList(
                new User("1", "张三", 1, new Date())
                , new User("2", "李四", 2, new Date())
        );
        log.info("执行了 searchList 方法:" + id);
        return list;
    }

    @Cacheable(value = "userobj", key = "'userobj'.concat(#id.toString())")
    public User searchObj(String id) {
        log.info("执行了 searchObj 方法:" + id);
        return SpringContextUtil.getBean(this.getClass()).redisCache(id);
    }

    @QueryCache
    public User redisCache(String id) {
        User user = new User("1", "张三", 1, new Date());
        log.info("执行了 redisCache 方法:" + id);
        return user;
    }
}
